﻿/**
*@description 人员设定
*/
Chenxin.UI.EmpSettingForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/OrganizationMgr.ashx',
	id:'usersettingWin',
	renderTo:'jqm_setting',
	title:'人员设定',
	jqmWidth:800,
	initFormValidator:function(){
		var _this=this;
		$('#'+this.id+'_usercode').formValidator({
			validatorgroup:this.id,
            onshow: "请输入用户名!", 
			onfocus: "请输入用户名!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "用户名不能为空!" 
			}, 
			onerror: "用户名不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: _this.actionUrl,
					data: { op: 'getuserbycode', usercode: val},
					onSuccess: function(data) {
						if(data.userinfo){
							$('#userinfo1').text(data.userinfo);
							_this.findByName('userid').val(data.UserId);
													
						}
						exist=data.exist;
					}
				});
				if (!exist) {
					$('#userinfo1').text('');
					return '用户名不存在';
				} else {
					return true;
				}
            },
            onerror: '用户名不存在！'
        });
		
		$('#'+this.id+'_aim_usercode').formValidator({
			validatorgroup:this.id,
            onshow: "请输入用户名!", 
			onfocus: "请输入用户名!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "用户名不能为空!" 
			}, 
			onerror: "用户名不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: _this.actionUrl,
					data: { op: 'getuserbycode', usercode: val},
					onSuccess: function(data) {
						if(data.userinfo){
							$('#userinfo2').text(data.userinfo);
							_this.findByName('aimuserid').val(data.UserId);													
						}
						exist=data.exist;
					}
				});
				if (!exist) {
					$('#userinfo2').text('');
					return '用户名不存在';
				} else {
					return true;
				}
            },
            onerror: '用户名不存在！'
        });
	},
	loadRole:function(companyid){
		var v='10';
		this.findByName('settingtype').each(function(i){
			if(this.checked){
				v=this.value;
			}
		});
		if(v=='10'){
			return;
		}
		var _this=this;
		Chenxin.ajax({
			url:this.actionUrl,
			type:'get',
			data:{op:'getrolebycompany',companyid:companyid},
			onSuccess:function(data){
				Chenxin.Fn.createCheckBoxGroup(data.items,'role',130,'td_'+_this.id+'_role');
			}
		});
	},
	createComboList:function(){
		var _this=this;
		//城市
        var souceCity = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_Souce_City',
            name: 'cityid',
            tbxWidth: 70,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    souceCompany.clearItems();
                } else {
                    souceCompany.param.fatherid = this.getValue();
                    souceCompany.reload();
                }
				souceBusiness.clearItems();
				souceDepart.clearItems();
            }
        });

        //公司
        var souceCompany = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
			inModelType:'none',
            containerId: '#' + this.id + '_Souce_Company',
            tbxWidth: 70,
			onSelected:function(){
                if (this.getValue() == this.defVal) {
                    souceBusiness.clearItems();
                } else {
                    souceBusiness.param.fatherid = this.getValue();
                    souceBusiness.reload();
                }
				souceDepart.clearItems();
			}
        });
		//商圈
		var souceBusiness = new Chenxin.UI.StructCombo({
            param: { op: 'getbusinessdistrict' },
            containerId: '#' + this.id + '_Souce_BusinessDistrict',
            tbxWidth: 70,
            colWidth: 120,
			autoLoad:false,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    souceDepart.clearItems();
                } else {
                    souceDepart.param.fatherid = this.getValue();
                    souceDepart.reload();
                }
            }
        });

        //部门
        var souceDepart = new Chenxin.UI.StructCombo({
            autoLoad: false,
            colWidth: 120,
            hasPage: true,
            pageSize: 10,
            hasSearch: true,
            param: { op: 'getdeparts' },
            name: 'depid',
            containerId: '#' + this.id + '_Souce_Depart',
            tbxWidth: 70
        });
		
		//城市
        var aimCity = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_Aim_City',
            name: 'aimcityid',
            tbxWidth: 70,
			txtModelName:'aimcity',
            inModelType: 'all',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    aimCompany.clearItems();
                } else {
                    aimCompany.param.fatherid = this.getValue();
                    aimCompany.reload();
                }
				aimBusiness.clearItems();
				aimDepart.clearItems();
            }
        });

        //公司
        var aimCompany = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
			inModelType:'all',
			txtModelName:'aimcompany',
			name:'aimcompanyid',
            containerId: '#' + this.id + '_Aim_Company',
            tbxWidth: 70,
			onSelected:function(){
                if (this.getValue() == this.defVal) {
                    aimBusiness.clearItems();
					$('#td_'+this.id+'_role').html('');
                } else {
                    aimBusiness.param.fatherid = this.getValue();
                    aimBusiness.reload();
					_this.loadRole(this.getValue());
                }
				aimDepart.clearItems();
			}
        });
		//商圈
		var aimBusiness = new Chenxin.UI.StructCombo({
            param: { op: 'getbusinessdistrict' },
            containerId: '#' + this.id + '_Aim_BusinessDistrict',
            tbxWidth: 70,
            colWidth: 120,
			autoLoad:false,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    aimDepart.clearItems();
                } else {
                    aimDepart.param.fatherid = this.getValue();
                    aimDepart.reload();
                }
            }
        });

        //部门
        var aimDepart = new Chenxin.UI.StructCombo({
            autoLoad: false,
            colWidth: 120,
            hasPage: true,
            pageSize: 10,
            hasSearch: true,
            param: { op: 'getdeparts' },
            name: 'aimdepid',
			txtModelName:'aimdepname',
			showMode:'multi',
			inModelType:'all',
			required:true,
            containerId: '#' + this.id + '_Aim_Depart',
            tbxWidth: 70
        });
		
		// var user=new Chenxin.UI.UserTreeCombo({
			// textBoxId:this.id+'_usercode',
			// name:'usercode',
			// hasSearch:false
		// });
		
		// var userAim=new Chenxin.UI.UserTreeCombo({
			// textBoxId:this.id+'_aim_usercode',
			// name:'aimusercode',
			// hasSearch:false
		// });

		
		return [souceCity,souceCompany,souceBusiness,souceDepart,aimCity,aimCompany,aimBusiness,aimDepart];
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){	
			if(_this.op=='add'){
				_this.save('/service/OrganizationMgr.ashx');
			}else{
				_this.save();			
			}
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
		
		var _depid=this.findByName('depid');
		var _aimdepid=this.findByName('aimdepid');
		
		var _usercode=this.findByName('usercode');
		var _aimusercode=this.findByName('aimusercode');
		_usercode.unFormValidator(true);
		_aimusercode.unFormValidator(true);
		
		this.findByName('settingtype').live('click',function(){
			if(this.value=='10'){
				$('#tr_empsetting').hide();	
				$('#tr_role').hide();	
				$('#tr_depsetting').show();
				$('#tr_numalsetting').show();
				$('#tr_refsetting').hide();
				_depid.required=true;
				_aimdepid.required=true;
				// _usercode.required=false;
				// _aimusercode.required=false;
				_usercode.unFormValidator(true);
				_aimusercode.unFormValidator(true);
				
				
				$('.tr_numal').show();
				_this.setWidth(800);
				
			}else if(this.value=='20'){
				$('#tr_empsetting').show();	
				$('#tr_numalsetting').show();
				$('#tr_role').show();
				$('#tr_depsetting').hide();
				$('#tr_refsetting').hide();
				
				_depid.required=false;
				_aimdepid.required=true;
				
				_usercode.unFormValidator(false);
				_aimusercode.unFormValidator(true);
				// _usercode.required=true;
				// _aimusercode.required=false;
				
				$('.tr_numal').show();
				_this.setWidth(800);
			}else{			
				$('#tr_empsetting').show();	
				$('#tr_role').hide();
				$('#tr_depsetting').hide();
				$('#tr_refsetting').show();
				$('#tr_numalsetting').hide();
				
				_depid.required=false;
				_aimdepid.required=false;
				
				
				_usercode.unFormValidator(false);
				_aimusercode.unFormValidator(false);
				// _usercode.required=true;
				// _aimusercode.required=true;
				
				$('.tr_numal').hide();
				_this.setWidth(500);
			}
		});
		
		Chenxin.Fn.createCheckBoxGroup(comboCommon.authority,'authority',60,'td_'+this.id+'_authority');
		Chenxin.Fn.createCheckBoxGroup(comboCommon.purview,'purview',130,'td_'+this.id+'_purview');
	},
	createForm:function(){
		return Chenxin.FormCommon.empSettingForm(this.id);
	},
	afterShow:function(){
		
		$('#tr_empsetting').hide();	
		$('#tr_role').hide();	
		$('#tr_depsetting').show();
		$('#tr_numalsetting').show();
		$('#tr_refsetting').hide();
		
		
		$('.tr_numal').show();
		$('#td_usersettingWin_role').text('');
		$('#span_userinfo').text('');
		
		
		this.findByName('depid').required=true;
		
		
		this.findByName('usercode').unFormValidator(true);
		this.findByName('aimusercode').unFormValidator(true);
		
		this.setWidth(800);
		this.reset();
	},
	customerFormVal:function(model){
		if(model.settingtype!='30'&&!model.purview){
			alert('请选择权限设定！');
			return false;
		}
		return true;
	},
	initComponent:function(){
		Chenxin.UI.EmpSettingForm.superclass.initComponent.call(this);
	}
	
});

/**
*@description 表单内容
*/
Chenxin.FormCommon.empSettingForm=function(comboIdPrefix){
return '<table class="tbl_f"><tr><th style="width:80px;">设定方式：</th><td><input type="radio" checked="checked" name="settingtype" value="10" />门店设定<input type="radio" name="settingtype" value="20" />个人设定<input type="radio" name="settingtype" value="30" />参照设定</td></tr><tr id="tr_depsetting"><th>源：</th><td><div class="fl">城市</div><div id="' + comboIdPrefix + '_Souce_City" class="SelectWrapSingle" style="z-index:505; margin-left:5px;"></div><div class="fl">公司</div><div id="' + comboIdPrefix + '_Souce_Company" class="SelectWrapSingle" style="z-index:504;margin-left: 5px;"></div><div class="fl">商圈(分类)</div><div id="' + comboIdPrefix + '_Souce_BusinessDistrict" class="SelectWrapSingle" style="z-index:503;margin-left: 5px;"></div><div class="fl">部门</div><div id="' + comboIdPrefix + '_Souce_Depart" class="SelectWrapSingle" style="z-index:502;margin-left: 5px;"></div></td></tr><tr id="tr_empsetting" style="display:none"><th>源工号：</th><td><input  id="' + comboIdPrefix + '_usercode" name="usercode" class="inputbox_n fl" type="text"  style="width:200px;margin-left: 5px;" /><div id="' + comboIdPrefix + '_usercodeTip" style="float:left"></div><div id="userinfo1" style="float:left;padding:4px 2px;"></div><input type="hidden" name="userid"/></td></tr><tr id="tr_refsetting" style="display:none; height:60px;"><th>目标工号：</th><td><div style="width:100%; height:30px;"><input  id="' + comboIdPrefix + '_aim_usercode"  name="aimusercode"  class="inputbox_n fl" type="text"  style="width:200px;margin-left: 5px;" /><div id="' + comboIdPrefix + '_aim_usercodeTip" style="float:left"></div><div id="userinfo2" style="float:left;padding:4px 2px;"></div><input type="hidden" name="aimuserid"/></div><div class="isfilled" style="width:100%;padding-left:5px;">将目标工号人员的业务设定复制给源工号人员</div></td></tr><tr id="tr_numalsetting"><th style="width:70px;">目标：</th><td><div class="fl">城市</div><div id="' + comboIdPrefix + '_Aim_City" class="SelectWrapSingle" style="z-index:501; margin-left:5px;"></div><div class="fl">公司</div><div id="' + comboIdPrefix + '_Aim_Company" class="SelectWrapSingle" style="z-index:500;margin-left: 5px;"></div><div class="fl">商圈(分类)</div><div id="' + comboIdPrefix + '_Aim_BusinessDistrict" class="SelectWrapSingle" style="z-index:499;margin-left: 5px;"></div><div class="fl">部门</div><div id="' + comboIdPrefix + '_Aim_Depart" class="SelectWrapSingle" style="z-index:498;margin-left: 5px;"></div></td></tr><tr class="tr_numal"><td colspan="2"><div class="divline"></div></td></tr><tr class="tr_numal"><th>岗位设定：</th><td id="td_' + comboIdPrefix + '_authority"></td></tr><tr class="tr_numal"><td colspan="2"><div class="divline"></div></td></tr><tr class="tr_numal"><th>权限设定：</th><td id="td_' + comboIdPrefix + '_purview"></td></tr><tr class="tr_numal"><td colspan="2"><div class="divline"></div></td></tr><tr class="tr_numal"><th>冲突解决：</th><td><input type="radio" value="10" name="sub"/>重置：将选中设置替换目标部门中的原有设置。如无，则新增设置。<br/> <input type="radio" value="20" name="sub"/>追加：将选中设置在目标部门中的原有设置上添加，其他设置保持。如无，则新增设置。<br/> <input type="radio" value="30" name="sub"/><span style=" color:red;">剔除：将选中设置从目标部门中的原有设置上去掉，其他设置保持。</span></td></tr><tr class="tr_numal"><td colspan="2"><div class="divline"></div></td></tr><tr id="tr_role" style="display:none;"><th>角色设定：</th><td id="td_' + comboIdPrefix + '_role"></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>';
}
